整合Swagger

  1. 依赖

    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-boot-starter</artifactId>
      <version>3.0.0</version>
    </dependency>
    
  2. 配置

    @Configuration
    @EnableOpenApi
    public class SwaggerConfig {
    
        @Bean
        public Docket docket(Environment environment) {
            // 仅在测试环境中开启
            boolean flag = environment.acceptsProfiles(Profiles.of("dev"));
            return new Docket(DocumentationType.OAS_30)
                    .apiInfo(apiInfo())
                    .enable(flag)
                    .groupName("default")
                    .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.any())
                    .build();
        }
    
        private ApiInfo apiInfo() {
            Contact contact = new Contact("陈广生", "https://github.com/rootwhois", "[email protected]");
            return new ApiInfo("个人博客管理系统API文档",
                    "永远相信,美好的事情即将发生。",
                    "1.0",
                    null,
                    contact,
                    null,
                    null,
                    new ArrayList<>());
        }
    }
    
  3. 使用

    1. 实体类上

      @ApiModel(value = "Admin对象", description = "")
      public class Admin implements Serializable {
      
          private static final long serialVersionUID = 1L;
      
          @ApiModelProperty("用户id")
          private Integer userId;
      
      }
      

注解使用:https://www.cnblogs.com/three-fighter/p/12346184.html

文档地址:http://localhost:8080/swagger-ui/index.html

1. 其他

  1. 使用Knife4j时报错:Failed to start bean ‘documentationPluginsBootstrapper‘; nested exception is java.lang.NullPointerException的解决方法:

    spring:
      mvc:
        pathmatch:
          matching-strategy: ANT_PATH_MATCHER
    
Copyright © rootwhois.cn 2021-2022 all right reserved,powered by GitbookFile Modify: 2023-03-05 10:55:52

results matching ""

    No results matching ""